<?php

namespace App\Store\Controllers\Goods;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Common\Services\Store\Common as StoreCommon;
use App\Store\Models\Goods\GoodsReplenish;
use App\Store\Models\Goods\GoodsReplenishInfolist;
use App\Store\Models\Goods\GoodsStockin;
use App\Store\Models\Goods\GoodsStockinInfolist;
use App\Store\Models\Goods\GoodsStockList;
use Illuminate\Support\Facades\Redis;

class GoodsPrinttagController extends Controller
{

    /**
     * 标签打印
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function printtag(Request $request)
    {
        $type = intval($request->type);
        $returntype = $request->returntype ?? 1;
        $data = [];
        $cp_id = StoreCommon::getCurrentAccountCompanyId($request);
        if ($type == 1) {
            $id = $request->id ?? 0;
            $ids = $request->ids ? $request->ids : [];
            $ids = is_array($ids) ? $ids : json_decode($ids, true);

            if (!empty($ids) || $id > 0) {
                $ids = GoodsStockin::query()
                    ->where(['cp_id' => $cp_id])
                    ->when(!empty($ids), function ($query) use ($ids) {
                        $query->whereIn('id', $ids);
                    })
                    ->when($id > 0, function ($query) use ($id) {
                        $query->where('id', $id);
                    })
                    ->get()
                    ->pluck('id')
                    ->toArray();
            }

            $query = GoodsStockinInfolist::query()->with(['goods_category', 'goods_style', 'goods_craft', 'goods_pricetype', 'goods_brand', 'goods_content'])
                ->when(!empty($ids),function ($query)use ($ids){
                    $query->whereIn('p_id', $ids);
                })
                ->where(['cp_id' => $cp_id]);
            $goods_style_ids = $request->goods_style_ids ? $request->goods_style_ids : [];
            $goods_style_ids = is_array($goods_style_ids) ? $goods_style_ids : json_decode($goods_style_ids, true);
            if (count($goods_style_ids) > 0) {
                $query->whereIn('goods_style_id', $goods_style_ids);
            }
            $goods_category_ids = $request->goods_category_ids ? $request->goods_category_ids : [];
            $goods_category_ids = is_array($goods_category_ids) ? $goods_category_ids : json_decode($goods_category_ids, true);
            if (count($goods_category_ids) > 0) {
                $query->whereIn('goods_category_id', $goods_category_ids);
            }
            $info_ids = $request->info_ids ? $request->info_ids : [];

            $info_ids = is_array($info_ids) ? $info_ids : json_decode($info_ids, true);
            if (count($info_ids) > 0) {
                $query->whereIn('id', $info_ids);
            }
            //获取分类查询信息
            $list = $query->get()->toArray();

            foreach ($list as $val) {
                $data[] = [
                    'goods_sn_code' => $val['goods_sn_code'],
                    'goods_name' => $val['goods_name'],
                    'style_name' => empty($val['goods_style']) ? '' : $val['goods_style']['style_name'],
                    'category_name' => $val['goods_category']['category_name'],
                    'content' => $val['goods_content']['content_name'] ?? '',
                    'piece_weight' => (float)$val['piece_weight'],
                    'netglod_weight' => (float)$val['netglod_weight'],
                    'netglod_weight2' => (int)sprintf('%d', (float)$val['netglod_weight'] * 1000),
                    'pieceprice_fee' => $val['pieceprice_fee'] / 100,
                    'gramprice_fee' => $val['gramprice_fee'] / 100,
                    'label_price' => $val['label_price'] / 100,
                    'craft_name' => empty($val['goods_craft']) ? '' : $val['goods_craft']['craft_name'],
                    'c_no' => $val['c_no'],
                    's_no' => $val['s_no'],
                    'gemstone_fee1' => $val['gemstone_fee1'] / 100,
                    'gemstone_fee2' => $val['gemstone_fee2'] / 100,
                    'add_fee1' => $val['add_fee1'] / 100,
                    'add_fee2' => $val['add_fee2'] / 100,
                    'brand_name' => empty($val['goods_brand']) ? '' : $val['goods_brand']['brand_name'],
                    'cf_no' => $val['cf_no'],
                    'cf_no_gia' => $val['cf_no_gia'],
                    'main_stone_weigth' => $val['main_stone_weigth'],
                    'main_stone_count' => $val['main_stone_count'],
                    'other_stone_weigth' => $val['other_stone_weigth'],
                    'other_stone_count' => $val['other_stone_count'],
                    'color' => $val['color'],
                    'clarity' => $val['clarity'],
                    'pricetype_name' => empty($val['goods_pricetype']) ? '' : $val['goods_pricetype']['pricetype_name'],
                    'remark' => $val['remark'],
                    'code_url' => env('TAG_CODE_URL') . '?type=' . $type . '&id=' . $val['id'],
                ];
            }
        } elseif ($type == 2) {
            //暂未支持
            // return response()->json(['code' => '001', 'msg' => '参数错误暂未支持']);
            $query = GoodsStockList::with(['goods_category', 'goods_style', 'goods_craft', 'goods_pricetype', 'goods_brand', 'goods_content'])->where(['cp_id' => $cp_id]);
            $ids = $request->ids ? $request->ids : [];

            $ids = is_array($ids) ? $ids : json_decode($ids, true);
            if (count($ids) > 0) {
                $query->whereIn('id', $ids);
                $list = $query->get()->toArray();

                foreach ($list as $val) {
                    $data[] = [
                        'goods_sn_code' => $val['goods_sn_code'],
                        'goods_name' => $val['goods_name'],
                        'style_name' => empty($val['goods_style']) ? '' : $val['goods_style']['style_name'],
                        'category_name' => $val['goods_category']['category_name'],
                        'content' => $val['goods_content']['content_name'] ?? '',
                        'piece_weight' => (float)$val['piece_weight'],
                        'netglod_weight' => (float)$val['netglod_weight'],
                        'netglod_weight2' => (int)sprintf('%d', (float)$val['netglod_weight'] * 1000),
                        'pieceprice_fee' => $val['pieceprice_fee'] / 100,
                        'gramprice_fee' => $val['gramprice_fee'] / 100,
                        'label_price' => $val['label_price'] / 100,
                        'craft_name' => empty($val['goods_craft']) ? '' : $val['goods_craft']['craft_name'],
                        'c_no' => $val['c_no'],
                        's_no' => $val['s_no'],
                        'gemstone_fee1' => $val['gemstone_fee1'] / 100,
                        'gemstone_fee2' => $val['gemstone_fee2'] / 100,
                        'add_fee1' => $val['add_fee1'] / 100,
                        'add_fee2' => $val['add_fee2'] / 100,
                        'brand_name' => empty($val['goods_brand']) ? '' : $val['goods_brand']['brand_name'],
                        'cf_no' => $val['cf_no'],
                        'cf_no_gia' => $val['cf_no_gia'],
                        'main_stone_weigth' => $val['main_stone_weigth'],
                        'main_stone_count' => $val['main_stone_count'],
                        'other_stone_weigth' => $val['other_stone_weigth'],
                        'other_stone_count' => $val['other_stone_count'],
                        'color' => $val['color'],
                        'clarity' => $val['clarity'],
                        'pricetype_name' => empty($val['goods_pricetype']) ? '' : $val['goods_pricetype']['pricetype_name'],
                        'remark' => $val['remark'],
                        'code_url' => env('TAG_CODE_URL') . '?type=' . $type . '&id=' . $val['id'],
                    ];
                }
            }
            //获取分类查询信息

        } elseif ($type == 3) {
            //补货
            $ids = $request->ids ? $request->ids : [];
            $ids = is_array($ids) ? $ids : json_decode($ids, true);

            $id = $request->id ?? 0;
            if (!empty($ids) || $id > 0) {
                $ids = GoodsReplenish::query()
                    ->where(['cp_id' => $cp_id, 'is_del' => 1])
                    ->when(!empty($ids), function ($query) use ($ids) {
                        $query->whereIn('id', $ids);
                    })
                    ->when($id > 0, function ($query) use ($id) {
                        $query->where('id', $id);
                    })
                    ->get()
                    ->pluck('id')
                    ->toArray();
            }

            $query = GoodsReplenishInfolist::query()->with(['goods_category', 'goods_style', 'goods_craft', 'goods_pricetype', 'goods_brand'])->where(['cp_id' => $cp_id]);

            $goods_style_ids = $request->goods_style_ids ? $request->goods_style_ids : [];
            $goods_style_ids = is_array($goods_style_ids) ? $goods_style_ids : json_decode($goods_style_ids, true);
            if (count($goods_style_ids) > 0) {
                $query->whereIn('goods_style_id', $goods_style_ids);
            }
            $goods_category_ids = $request->goods_category_ids ? $request->goods_category_ids : [];
            $goods_category_ids = is_array($goods_category_ids) ? $goods_category_ids : json_decode($goods_category_ids, true);
            if (count($goods_category_ids) > 0) {
                $query->whereIn('goods_category_id', $goods_category_ids);
            }
            $info_ids = $request->info_ids ? $request->info_ids : [];

            $info_ids = is_array($info_ids) ? $info_ids : json_decode($info_ids, true);

            if (count($info_ids) > 0) {
                $query->whereIn('id', $info_ids);
            }if (count($ids) > 0) {
                $query->whereIn('p_id', $ids);
            }
            //获取分类查询信息
            $list = $query->get()->toArray();
            foreach ($list as $val) {

                $data[] = [
                    'goods_sn_code' => $val['goods_sn_code'],
                    'goods_name' => $val['goods_name'],
                    'style_name' => empty($val['goods_style']) ? '' : $val['goods_style']['style_name'],
                    'category_name' => empty($val['goods_category']) ? '' : $val['goods_category']['category_name'],
                    'content' => $val['content'] ?? '',
                    'piece_weight' => (float)$val['piece_weight'],
                    'netglod_weight' => (float)$val['netglod_weight'],
                    'netglod_weight2' => (int)sprintf('%d', (float)$val['netglod_weight'] * 1000),
                    'pieceprice_fee' => $val['pieceprice_fee'] / 100,
                    'gramprice_fee' => $val['gramprice_fee'] / 100,
                    'label_price_new' => $val['label_price_new'] / 100,
                    'craft_name' => empty($val['goods_craft']) ? '' : $val['goods_craft']['craft_name'],
                    'c_no' => $val['c_no'],
                    's_no' => $val['s_no'],
                    'gemstone_fee1' => $val['gemstone_fee1'] / 100,
                    'gemstone_fee2' => $val['gemstone_fee2'] / 100,
                    'add_fee1' => $val['add_fee1'] / 100,
                    'add_fee2' => $val['add_fee2'] / 100,
                    'brand_name' => empty($val['goods_brand']) ? '' : $val['goods_brand']['brand_name'],
                    'cf_no' => $val['cf_no'],
                    'cf_no_gia' => $val['cf_no_gia'],
                    'main_stone_weigth' => $val['main_stone_weigth'],
                    'main_stone_count' => $val['main_stone_count'],
                    'other_stone_weigth' => $val['other_stone_weigth'],
                    'other_stone_count' => $val['other_stone_count'],
                    'color' => $val['color'],
                    'clarity' => $val['clarity'],
                    'pricetype_name' => empty($val['goods_pricetype']) ? '' : $val['goods_pricetype']['pricetype_name'],
                    'remark' => $val['remark'],
                    'code_url' => env('TAG_CODE_URL') . '?type=' . $type . '&id=' . $val['id'],
                ];

            }
        }

        if ($returntype == 2) {
            return response()->json(['code' => '000', 'msg' => '成功', 'data' => ['jsondata' => $data]])->setEncodingOptions(JSON_UNESCAPED_UNICODE);
        } else {
            return response()->json(['code' => '000', 'msg' => '成功', 'data' => ['jsondata' => json_encode($data, JSON_UNESCAPED_UNICODE)]]);
        }

    }

    /**
     * 组件标签打印
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function exePrinttag(Request $request)
    {
        $type = intval($request->type);
        $temp_id = intval($request->temp_id);
        if (empty($temp_id)) {
            return response()->json(['code' => '001', 'msg' => '打印的模板id不能为空']);
        }
        $cp_id = StoreCommon::getCurrentAccountCompanyId($request);
        // 单条
        $id = $request->id ?? 0;
        // 多条
        $ids = $request->ids ? $request->ids : [];
        $ids = is_array($ids) ? $ids : json_decode($ids, true);
        // 样式
        $goods_style_ids = $request->goods_style_ids ? $request->goods_style_ids : [];
        $goods_style_ids = is_array($goods_style_ids) ? $goods_style_ids : json_decode($goods_style_ids, true);
        // 分类
        $goods_category_ids = $request->goods_category_ids ? $request->goods_category_ids : [];
        $goods_category_ids = is_array($goods_category_ids) ? $goods_category_ids : json_decode($goods_category_ids, true);
        // 详情的id
        $info_ids = $request->info_ids ? $request->info_ids : [];
        $info_ids = is_array($info_ids) ? $info_ids : json_decode($info_ids, true);

        $stockIds = [];
        if ($type == 1) {
            if (!empty($ids) || $id > 0) {
                $ids = GoodsStockin::query()
                    ->where(['cp_id' => $cp_id])
                    ->when(!empty($ids), function ($query) use ($ids) {
                        $query->whereIn('id', $ids);
                    })
                    ->when($id > 0, function ($query) use ($id) {
                        $query->where('id', $id);
                    })
                    ->get()
                    ->pluck('id')
                    ->toArray();
            }

            $stockIds = GoodsStockinInfolist::query()
                ->where(['cp_id' => $cp_id])
                ->when(!empty($info_ids), function ($query) use ($info_ids) {
                    $query->whereIn('id', $info_ids);
                })
                ->when(!empty($ids), function ($query) use ($ids) {
                    $query->whereIn('p_id', $ids);
                })
                ->when(!empty($goods_style_ids), function ($query) use ($goods_style_ids) {
                    $query->whereIn('goods_style_id', $goods_style_ids);
                })
                ->when(!empty($goods_category_ids), function ($query) use ($goods_category_ids) {
                    $query->whereIn('goods_category_id', $goods_category_ids);
                })
                ->get()
                ->pluck('id')
                ->toArray();
        } elseif ($type == 2 && !empty($ids)) {
            $stockIds = GoodsStockList::query()
                ->where(['cp_id' => $cp_id, 'is_del' => 1])
                ->when(count($ids) > 0, function ($query) use ($ids) {
                    $query->whereIn('id', $ids);
                })
                ->get()
                ->pluck('id')
                ->toArray();
        } elseif ($type == 3) {
            if (!empty($ids) || $id > 0) {
                $ids = GoodsReplenish::query()
                    ->where(['cp_id' => $cp_id, 'is_del' => 1])
                    ->when(!empty($ids), function ($query) use ($ids) {
                        $query->whereIn('id', $ids);
                    })
                    ->when($id > 0, function ($query) use ($id) {
                        $query->where('id', $id);
                    })
                    ->get()
                    ->pluck('id')
                    ->toArray();
            }

            //补货打印
            $stockIds = GoodsReplenishInfolist::query()
                ->where(['cp_id' => $cp_id, 'is_del' => 1])
                ->when(count($ids) > 0, function ($query) use ($ids) {
                    $query->whereIn('p_id', $ids);
                })
                ->when(count($info_ids) > 0, function ($query) use ($info_ids) {
                    $query->whereIn('id', $info_ids);
                })
                ->when(!empty($goods_style_ids), function ($query) use ($goods_style_ids) {
                    $query->whereIn('goods_style_id', $goods_style_ids);
                })
                ->when(!empty($goods_category_ids), function ($query) use ($goods_category_ids) {
                    $query->whereIn('goods_category_id', $goods_category_ids);
                })
                ->get()
                ->pluck('id')
                ->toArray();
        }

        $stockIds = implode(',', $stockIds);
        if (empty($stockIds)) {
            return response()->json(['code' => '001', 'msg' => '数据不存在']);
        }

        $url = env('APP_URL') . "/store/basic/templatetag/tagPrint?id=$temp_id&type=$type&cp_id=$cp_id&stockIds=$stockIds";

        return response()->json(['code' => '000', 'msg' => '成功', 'data' => ['url' => $url]]);

    }

    public function gettagcodeinfo(Request $request)
    {
        $type = intval($request->type);
        $id = intval($request->id);

        if ($type <= 0 || $id <= 0) {
            return response()->json(['code' => '001', 'msg' => '参数错误']);
        }
        $data = [];
        if ($type == 1) {
            $info = GoodsStockinInfolist::with(['goods_category', 'storeinfo', 'goods_style','goods_content'])->where(['id' => $id])->first();
        } elseif ($type == 2) {

            $info = GoodsStockList::with(['goods_category', 'storeinfo', 'goods_style','goods_content'])->where(['id' => $id])->first();
        } elseif ($type == 3) {//补货
            $info = GoodsReplenishInfolist::with(['goods_category', 'storeinfo', 'goods_style'])->where(['id' => $id])->first();
        }

        if (!$info) {
            return response()->json(['code' => '001', 'msg' => '商品已失效']);
        }
        $info = $info->toArray();

        //所属门店、条码、品名、商品类别、证书号、款式、金重、件重、含量；图片上放这些字段
        //过滤字段
        if($type!=3){
            $content =$info['goods_content']['content_name']??'';
        }else{
            $content =$info['content']??'';
        }

        $returninfo = [];
        $returninfo['storeinfo'] = [
            'store_name' => $info['storeinfo']['store_name'],
            'store_file_path' => $info['storeinfo']['store_file_path'] ? json_decode($info['storeinfo']['store_file_path'], true) : []
        ];
        $returninfo['tracing_url'] = $info['storeinfo']['tracing_url'] ?? '';
        $returninfo['goods_sn_code'] = $info['goods_sn_code'];
        $returninfo['goods_name'] = $info['goods_name'];
        $returninfo['category_name'] = $info['goods_category']['category_name'];
        $returninfo['cf_no'] = $info['cf_no'];
        $returninfo['style_name'] = $info['goods_style'] ? $info['goods_style']['style_name'] : '';
        $returninfo['netglod_weight'] = isset($info['netglod_weight_new']) ? $info['netglod_weight_new'] : $info['netglod_weight'];
        $returninfo['piece_weight'] = isset($info['piece_weight_new']) ? $info['piece_weight_new'] : $info['piece_weight'];
        $returninfo['content'] = $content;
        $returninfo['goods_img'] = $info['goods_img'];
        return response()->json(['code' => '000', 'msg' => '成功', 'data' => ['info' => $returninfo]]);
    }
}
